package Distributive;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JOptionPane;

import Gui.mainGui;
import cards.Card;

/**
 * 
 * @author CAT
 * 
 */

public class casinoRoyale {

	private Socket gameSocket;
	private ObjectOutputStream output;
	// private ObjectInputStream input;
	public static ArrayList<String> cardList;
	Scanner keyboard;
	int count;
	String s;

	/**
	 * Initializes new Scanner, keyboard. Initializes string s to something. If
	 * Gui has previously not been run. Run the maiGui and sets the window to
	 * visible. Initialized allCards dealt to cardList. And sets s to
	 * cardList.toString()
	 * 
	 * @param hasBeenRun
	 * @throws IOException
	 */
	public casinoRoyale(boolean hasBeenRun) throws IOException {
		this.keyboard = new Scanner(System.in);
		s = "";
		if (!hasBeenRun) {
			mainGui window = new mainGui();
			window.setVisible(true);
			cardList = window.allCards;
			for (String hand : cardList){
				s+= hand;
			}
			
		}
	}

	/**
	 * Creates new casinoRoyal called theProducer. Calls the executeClient() on
	 * theProducer.
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		casinoRoyale theProducer = new casinoRoyale(true);
		System.out.println("Casino Active");
		theProducer.executeClient();
	}

	/**
	 * Initialized gameSocket to a new Socket Initialized the
	 * ObjectOutputStream(); Closes the output and the gameSocket.
	 * 
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public void executeClient() throws UnknownHostException, IOException {
		try {
			System.out.println("*****inside try");
			System.out.println(s);
			this.gameSocket = new Socket("127.0.0.1", 8189);
			this.output = new ObjectOutputStream(this.gameSocket
					.getOutputStream());
			// output.writeObject(sendStuff());
		} catch (java.net.SocketException e) {
			JOptionPane.showMessageDialog(null, "inside catch");
			System.out.println();
			System.out.println("Producer quitting");
		}
		this.output.close();
		this.gameSocket.close();

	}

	/**
	 * 
	 * @return returns the cardList()
	 */
	public String sendCards() {
		return s;
	}
}
